package com.a214.yxz.yuejian013.Activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Base.BaseSActivity;
import com.a214.yxz.yuejian013.JavaBean.FollowBean;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Model.ResModel;
import com.a214.yxz.yuejian013.R;
import com.a214.yxz.yuejian013.Adapter.FollowListAdapter;

import java.util.List;

public class UserFollowActivity extends BaseSActivity {

    private ImageButton return_btn;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private FollowListAdapter adapter;
    private ResModel model=new ResModel("gson",getSessionID());
    private ServiceListener<List<FollowBean>> listener;
    private ResModel c_model=new ResModel("",getSessionID());
    private ServiceListener<String> col_listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_follow);

        return_btn= (ImageButton) findViewById(R.id.user_follow_return_btn);
        recyclerView= (RecyclerView) findViewById(R.id.user_follow_list);
        layoutManager=new LinearLayoutManager(UserFollowActivity.this);
        adapter=new FollowListAdapter(UserFollowActivity.this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);

        listener=new ServiceListener<List<FollowBean>>() {
            @Override
            public void onResponse(final List<FollowBean> l) {
                adapter.setList(l);
                adapter.setOnItemClickListener(new FollowListAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        c_model.uncollect(l.get(position).getFan_id(),getUserId(),l.get(position).getFans_type(),col_listener);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(UserFollowActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };
        col_listener=new ServiceListener<String>() {
            @Override
            public void onResponse(String l) {
                Toast.makeText(UserFollowActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
                getFollowList();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(UserFollowActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };

        return_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        getFollowList();
    }
    private void getFollowList(){
        model.getList("fans",getUserId(),1,"insert_time",listener);
    }
}
